<?php

class Form_Articles extends Cms_FormProcessor
{
    protected $db = null;               // объект для соединения с базой данных
    public $articles = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id = 0) {
        parent::__construct();
        
        $this->db = $db;
        $this->articles = new Model_articles($this->db);
        if ($id!='0'){
        $this->articles->load($id);
        $this->id = $this->articles->getId();
        $this->name = $this->articles->name;
        $this->h1 = $this->articles->h1;
        $this->description = $this->articles->description;
        $this->keywords = $this->articles->keywords;
        $this->alias = $this->articles->alias;
        $this->short = $this->articles->short;
        $this->full = $this->articles->full;
      
        }
        
    }
    
    public function process(Zend_Controller_Request_Abstract $request) {
        // проверим название
        $this->name = trim($request->getPost('name'));

        if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->articles->name = $this->name;
        
        $this->h1 = trim($request->getPost('h1'));

        if (strlen($this->h1) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->articles->h1 = $this->h1;
        
        $this->description = trim($request->getPost('description'));

        if (strlen($this->description) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->articles->description = $this->description;
        
        $this->keywords = trim($request->getPost('keywords'));

        if (strlen($this->keywords) == 0)
            $this->addError('keywords', 'Вы должны указать название');
        else
            $this->articles->keywords = $this->keywords;
        
        $this->alias = trim($request->getPost('alias'));

        if (strlen($this->alias) == 0)
            $this->addError('alias', 'Вы должны указать название');
        else
            $this->articles->alias = $this->alias;
        
        $this->short = trim($request->getPost('short'));

        if (strlen($this->short) == 0)
            $this->addError('short', 'Вы должны указать название');
        else
            $this->articles->short = $this->short;
        
        $this->full = trim($request->getPost('full'));

        if (strlen($this->full) == 0)
            $this->addError('full', 'Вы должны указать название');
        else
            $this->articles->full = $this->full;
        
            $this->articles->date =  date("Y.m.d h:i:s");
        
        
        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->articles->save();
            unset($session->phrase);
        }
        
        return !$this->hasError();
    }
}
